package com.ruanda.spongebobrunner.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.ruanda.spongebobrunner.R;

public class LoadingView extends View {
    private Paint paint;
    private Paint textPaint;
    private Paint progressTextPaint;
    private int progress = 0;
    private String[] loadingTexts = { "正在加载资源...", "正在初始化游戏...", "即将开始..." };
    private int currentTextIndex = 0;
    private Bitmap appIcon;

    public LoadingView(Context context) {
        super(context);
        init();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(48);
        textPaint.setTextAlign(Paint.Align.CENTER);

        progressTextPaint = new Paint();
        progressTextPaint.setAntiAlias(true);
        progressTextPaint.setColor(Color.BLACK);
        progressTextPaint.setTextSize(36);
        progressTextPaint.setTextAlign(Paint.Align.LEFT);

        // 加载应用图标
        try {
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.ic_launcher);
            if (drawable != null) {
                appIcon = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
                android.graphics.Canvas canvas = new android.graphics.Canvas(appIcon);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制背景
        canvas.drawColor(Color.WHITE);

        // 绘制应用图标
        if (appIcon != null) {
            // 计算图标绘制位置（屏幕上方1/3处居中）
            int iconSize = Math.min(getWidth() / 2, getHeight() / 2);
            float iconScale = (float) iconSize / Math.max(appIcon.getWidth(), appIcon.getHeight());
            int scaledWidth = (int) (appIcon.getWidth() * iconScale);
            int scaledHeight = (int) (appIcon.getHeight() * iconScale);
            int iconX = (getWidth() - scaledWidth) / 2;
            int iconY = getHeight() / 6; // 屏幕上方1/6处

            Rect srcRect = new Rect(0, 0, appIcon.getWidth(), appIcon.getHeight());
            Rect dstRect = new Rect(iconX, iconY, iconX + scaledWidth, iconY + scaledHeight);
            canvas.drawBitmap(appIcon, srcRect, dstRect, paint);
        }

        // 绘制进度条背景
        paint.setColor(Color.LTGRAY);
        int progressBarWidth = getWidth() * 3 / 4; // 进度条占屏幕3/4宽度
        int progressBarHeight = 30;
        int progressBarTop = getHeight() * 4 / 5; // 进度条位置在屏幕下方1/3处
        int cornerRadius = 15; // 圆角半径

        // 绘制带圆角的进度条背景
        paint.setAntiAlias(true);
        RectF progressBarBackground = new RectF(
                (getWidth() - progressBarWidth) / 2,
                progressBarTop,
                (getWidth() + progressBarWidth) / 2,
                progressBarTop + progressBarHeight);
        canvas.drawRoundRect(progressBarBackground, cornerRadius, cornerRadius, paint);

        // 绘制进度条
        paint.setColor(Color.GREEN); // 修改为绿色
        RectF progressRect = new RectF(
                (getWidth() - progressBarWidth) / 2,
                progressBarTop,
                (getWidth() - progressBarWidth) / 2 + (int) (progressBarWidth * (progress / 100.0)),
                progressBarTop + progressBarHeight);
        canvas.drawRoundRect(progressRect, cornerRadius, cornerRadius, paint);

        // 绘制进度条下方的描述文本（增加间距）
        String loadingText = loadingTexts[currentTextIndex];
        canvas.drawText(loadingText, getWidth() / 2f, progressBarTop + progressBarHeight + 80, textPaint);

        // 绘制进度百分比（在进度条右侧）
        String progressText = progress + "%";
        progressTextPaint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(progressText, (getWidth() + progressBarWidth) / 2f + 20,
                getHeight() * 4f / 5f + progressBarHeight / 2f + 10, progressTextPaint);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setLoadingText(int textIndex) {
        if (textIndex >= 0 && textIndex < loadingTexts.length) {
            this.currentTextIndex = textIndex;
            invalidate();
        }
    }
}